--
-- Author: shineflag
-- Date: 2017-02-15 18:58:07
--

local skynet = require "skynet"
local log = require "log"
local proto = require("json_proto")

local levels = {}   --所有的场次

local CMD = {}


local conf = {
	--        --场次      名称      小盲     大盲      前注    最小携带        最大携带           下注时间     座位数     抽水比率     2人封顶          3-4人封顶       5人以上封顶
	-- [1] = {lv = 1, name="初级场",sb=5,    bb=10, 	ante=0, min_carry=200, 	max_carry=500,  	bettime=15, seatnum=6, fee_per=5,	fee_max_2=150, 	fee_max_34=150,  fee_max_5=150},
	-- [2] = {lv = 2, name="中级场",sb=25,   bb=50, 	ante=0, min_carry=1000,	max_carry=2500, 	bettime=15, seatnum=6, fee_per=5,	fee_max_2=500, 	fee_max_34=500,  fee_max_5=1000},
	-- [3] = {lv = 3, name="高级场",sb=100,  bb=200,	ante=0, min_carry=8000,	max_carry=20000,	bettime=15, seatnum=6, fee_per=5,	fee_max_2=1000,	fee_max_34=2000, fee_max_5=3000},
}

local function get_chan_ids(chan)
	local info_ver = skynet.uniqueservice "info"
	return skynet.call(info_ver, "lua", "get_chan_ids", "cash", chan)
end

function CMD.create_tablekeeper(conf)
	if levels[conf.lv] then
		log.e(TAG,"level [%d] exists",conf.lv)
	else
		local tablemanger = skynet.newservice "tablemanager"

		local keeper = {
			tnum = 0,
			player = 0,
			watcher = 0,
			manager = tablemanger
		}
		for key, v in pairs(conf) do 
			keeper[key] = v 
		end
		levels[conf.lv] = keeper
		skynet.call(tablemanger,"lua","init",skynet.self(), conf)
	end
end

function CMD.info(mid, data)
	local pkg = proto.unpack_body(data)
	local chan = pkg.req.chan

	local ver,lvids  = get_chan_ids(chan)
	local lvs = {}
	for _, lv in ipairs(lvids) do 
		if levels[lv] then 
			table.insert(lvs, levels[lv])
		else
			log.e(TAG,"not found cash lv[%d]",lv)
		end
	end
	return proto.create_resp("alloc.info",{ret=200, ver=ver, levels=lvs})
end

function CMD.query_table(lv, tid)
	local keeper = levels[lv]
	return skynet.call(keeper.manager, "lua", "query_table", tid)
end

--client 
function CMD.search(mid, data )
	local pkg = proto.unpack_body(data)
	local lv = pkg.req.lv 
	local keeper = levels[lv]
	if keeper then
		return skynet.call(keeper.manager,"lua","search",mid)
	else
		log.e(TAG,"bad lv[%d]",lv)
	end
end

--桌子上报人数 call by tablemanager 
function CMD.report_manager_info(lv, tnum, player,watcher)
	local keeper = levels[lv]
	if  keeper then
		keeper.tnum = tnum
		keeper.player = player
		keeper.watcher = watcher

		log.d(TAG,"lv[%d] now tnum[%d] player[%d] watcher[%d]",lv, tnum,player, watcher)
	else
		log.e("report but not found tid[%d]",tid)
	end
end

--获取各场次的人数  lv:相应场次 0代表所有场次 call by admin
function CMD.get_players_info(lv)
	local result = {}
	if lv == 0 then 
		for lv , keeper in pairs(levels) do 
			table.insert(result,{lv=lv,tnum=keeper.tnum, player=keeper.player, watcher=keeper.watcher})
		end
	else
		local keeper = levels[lv]
		if keeper then 
			table.insert(result,{lv=lv,tnum=keeper.tnum, player=keeper.player, watcher=keeper.watcher})
		end
	end
	return result
end

--获取各场次总玩牌的人数(在玩)   call by admin
function CMD.get_players_num()
	local total = 0
	for lv , keeper in pairs(levels) do 
		total = total + keeper.player
	end
	return total
end

----获取指定人数的桌子 
function CMD.get_num_tables(lv, num)
	local result = {}
	if lv == 0 then 
		for lv , keeper in pairs(levels) do 
			local list = skynet.call(keeper.manager, "lua", "query_num_tables",num)
			result[lv] = list
		end
	else
		local keeper = levels[lv]
		if keeper then 
			local list = skynet.call(keeper.manager, "lua", "query_num_tables",num)
			result[lv] = list
		end
	end
	return result
end

local function init()
	conf = require("cash_conf")
	for _, v in pairs(conf) do 
		CMD.create_tablekeeper(v)
	end
end

skynet.start(function()
	init()
	skynet.dispatch("lua", function(session,source,cmd,...)
		log.d(TAG,"recv cmd[%s]",cmd)
		local f = CMD[cmd]
		if f then
			if session ~= 0 then
				skynet.ret(skynet.pack(f(...)))
			else
				f( ... )
			end
		else
			log.e(TAG,"not found cmd[%s]",cmd)
		end
	end)

end)